package user;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.UTFDataFormatException;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;

import javax.xml.bind.DatatypeConverter;

import Encryption.Encryptor;

import security.servlet.SecurityServlet;

import db.PreparedQuery;
import db.Query;

public class BID {
	public static ArrayList<BID> bids = new ArrayList<BID>();
	public static final String TABLE_NAME = "BID";
	public static final String User_Name_FIELD_NAME = TABLE_NAME + ".Name";
	public static final String BID_FIELD_NAME = TABLE_NAME + ".BID";
	public static final String BID_VALUE_FIELD_NAME = TABLE_NAME + ".BidValue";

	public BID() {
	}

	public BID( String name , String value)
	{
		this.name = name;
		this.bidValue = value;
		
	}	
	private String name;
	private int bId;
	private String bidValue;

	public boolean load() throws Exception {
		bids = new ArrayList<BID>();
		String sql = "SELECT *" + "  FROM " + TABLE_NAME + "  ;";

		PreparedQuery q = new PreparedQuery();
		ResultSet rs = null;
		try {
			q.open(sql);

			rs = q.executeQuery();
			while (rs.next()) {
				byte i = 1;
				
				this.bId = rs.getInt(i++);
				this.name = rs.getString(i++);
				this.bidValue = rs.getString(i++);
				bids.add(new BID(name,bidValue));
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if (rs != null)
				rs.close();
			q.close();
		}
	}

	public boolean add() throws Exception {
		String sql = "INSERT INTO "+TABLE_NAME+" set " + User_Name_FIELD_NAME + " = ?,"
				+ BID_VALUE_FIELD_NAME  + " =?;";
		PreparedQuery q = new PreparedQuery();
		try {

			q.open(sql);
			q.setParameter(1, this.getName());
			q.setParameter(2, this.getBidValue());
			q.executeUpdate();
			// System.out.println(rs.toString());

			return true;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
			return false;
		} finally {
			q.close();
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getbId() {
		return bId;
	}

	public void setbId(int bId) {
		this.bId = bId;
	}

	public String getBidValue() {
		return bidValue;
	}

	public void setBidValue(String bidValue) {
		this.bidValue = bidValue;
	}

	@Override
	public String toString() {
		return "BID [name=" + name + ", bId=" + bId + ", bidValue=" + bidValue
				+ "]";
	}
	
	
}
